﻿using System;

class ExchangeBits
{
    static void Main()
    {
        //Exercise 13.
        //Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.

        Console.Write("Enter a positive integer: ");
        uint n = uint.Parse(Console.ReadLine());       
        Console.WriteLine("Original integer: " + Convert.ToString(n, 2).PadLeft(32, '0'));
        Console.WriteLine();
        uint mask = 7;
        uint bits345 = (7 << 3) & n;            //get bits 3,4,5
        Console.WriteLine(Convert.ToString(bits345, 2));

        uint bits2456 = (7 << 24) & n;          //get bits 24,25,26
        Console.WriteLine(Convert.ToString(bits2456, 2));

        bits345 = bits345 << 21;                //moving bits 3,4,5   21-positions to the left
        Console.WriteLine("Bits 3, 4 & 5:" + Convert.ToString(bits345, 2).PadLeft(32, '0'));

        bits2456 = bits2456 >> 21;              //moving bits 24,25,26    21-positions to the right
        Console.WriteLine("Bits 24, 25 & 26:" + Convert.ToString(bits2456, 2).PadLeft(32, '0'));

        n = n & (~(mask << 3));                 //null bits 3,4,5 to ease the merger
        Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
        n = n & (~(mask << 21));                //null bits 24,25,26 to ease the merger
        Console.WriteLine("BeforeChange:" + Convert.ToString(n, 2).PadLeft(32, '0'));
        n = n | bits345;                        //merge the number and the replaced bits 3,4,5
        Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
        n = n | bits2456;                       //merge the number and the replaced bits 24,25,26
        Console.WriteLine();

        Console.WriteLine("Transformed:" + Convert.ToString(n, 2).PadLeft(32, '0'));
        Console.WriteLine("Transformed: " + n);

    }
}

